package com.xingongchang.baolebei.activity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.external.androidquery.callback.AjaxStatus;
import com.insthub.BeeFramework.activity.BaseActivity;
import com.insthub.BeeFramework.model.BusinessResponse;
import com.xingongchang.baolebei.R;
import com.xingongchang.baolebei.model.ProtocolConst;
import com.xingongchang.baolebei.model.UserInfoModel;

public class CoinActivity extends BaseActivity implements BusinessResponse {

	private ImageView backButton;
	private TextView title;
	private UserInfoModel mUserInfoModel;
	TextView credit;
	TextView topic_rule;
	TextView letter_rule;
	SharedPreferences coin;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_coin);
		backButton = (ImageView) findViewById(R.id.top_view_back);
		title = (TextView) findViewById(R.id.top_view_text);
		credit = (TextView) findViewById(R.id.credit);
		topic_rule = (TextView) findViewById(R.id.topic_rule);
		letter_rule = (TextView) findViewById(R.id.letter_rule);
		backButton.setVisibility(View.VISIBLE);
		title.setText("我的金币");
		backButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		mUserInfoModel = new UserInfoModel(this);
		mUserInfoModel.addResponseListener(this);
		mUserInfoModel.getCoin();
		coin=getSharedPreferences("money",MODE_PRIVATE);
	}

	@Override
	public void OnMessageResponse(String url, JSONObject jo, AjaxStatus status)
			throws JSONException {
		// TODO Auto-generated method stub
		if (url.endsWith(ProtocolConst.COIN)) {
			JSONObject obj = jo.optJSONObject("data");
			String credits = obj.optString("credits");
			JSONArray adJsonArray = obj.optJSONArray("rule");
			String[] description = null;
			if (null != adJsonArray && adJsonArray.length() > 0) {
				description = new String[adJsonArray.length()];
				for (int i = 0; i < adJsonArray.length(); i++) {
					JSONObject adJsonObject = adJsonArray.getJSONObject(i);
					description[i] = adJsonObject.optString("description");
				}
			}
			coin.edit().putString("user_coin",credits).commit();
			credit.setText(credits);
			topic_rule.setText(description[0]);
			letter_rule.setText(description[1]);
			
		}
	}
}
